package com.zhonghan.common.utils;

import org.junit.Test;
import org.springframework.util.DigestUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 接口签名api测试
 * 签名数据按字母排序，
 * 用key1=value1&key2=value2...&keyn=valuen拼接
 * 对所有拼接value进行url编码
 * 拼接完成后使用md5(appSecret+str)获得签名
 * 拼接请求参数str&sign=sign
 * get请求最终得到的结果到服务器
 * 服务器接收get请求
 * 对参数进行相同逻辑签名
 * 判断签名是否与请求过来的签名一致
 * 一致说明请求数据没有被修改
 */
public class ZHApiSign {

    private final static String appId = "appId";

    private static final String APP_KEY ="10001782";
	private static final String APP_SECRET = "IKtXMSpY2rIxG2dG4KQb1mDEPPg2lLTP";

    /**
     * 签名
     * @param appId
     * @param appKey
     * @param appSecret
     * @param type
     * @param money
     * @return
     */
    private String sign(String gasnumber, 
			String gasname, 
			String gasphone, 
			String gastype, 
			Integer money, String appKey, String appSecret) {
        Map<String, String> data = new HashMap<>();
        data.put("gasnumber", gasnumber);
        data.put("gasname", gasname);
        data.put("gasphone", gasphone);
        data.put("gastype", gastype);
        data.put("money", String.valueOf(money));
        data.put("appKey", appKey);
        String[] array  = data.keySet().toArray(new String[data.size()]);
        Arrays.sort(array);
        StringBuilder builder = new StringBuilder();
        for (String key : array) {
            try {
                builder.append(key).append("=").append(URLEncoder.encode(data.get(key), "UTF-8")).append("&");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        String sign = DigestUtils.md5DigestAsHex((appSecret + builder.toString()).getBytes());
        return builder.append("sign=").append(sign).toString();
    }

    /**
     * 参数校验
     * @param gasnumber
     * @param gasname
     * @param gasphone
     * @param gastype
     * @param money
     * @param appKey
     * @param appSecret
     * @param sign
     * @return
     */
    public static boolean check( 
    		String gasnumber, 
			String gasname, 
			String gasphone, 
			String gastype, 
			Integer money, String sign) {
        Map<String, String> data = new HashMap<>();
        data.put("gasnumber", gasnumber);
        data.put("gasname", gasname);
        data.put("gasphone", gasphone);
        data.put("gastype", gastype);
        data.put("money", String.valueOf(money));
        data.put("appKey", APP_KEY);
        String[] array  = data.keySet().toArray(new String[data.size()]);
        Arrays.sort(array);
        StringBuilder builder = new StringBuilder();
        for (String key : array) {
            try {
                builder.append(key).append("=").append(URLEncoder.encode(data.get(key), "UTF-8")).append("&");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return DigestUtils.md5DigestAsHex((APP_SECRET + builder.toString()).getBytes()).equals(sign);
    }
    
    /**
     * 
     * @param gasnumber
     * @param gasname
     * @param gasphone
     * @param orderId
     * @param money
     * @param sign
     * @return
     */
    public static boolean check2( 
    		String appkey,
    		String appSecret,
    		String gasnumber, 
			String gasname, 
			String gasphone, 
			String orderId, 
			Integer money, String sign) {
        Map<String, String> data = new HashMap<>();
        data.put("gasnumber", gasnumber);
        data.put("gasname", gasname);
        data.put("gasphone", gasphone);
        data.put("orderId", orderId);
        data.put("money", String.valueOf(money));
        data.put("appKey", appkey);
        String[] array  = data.keySet().toArray(new String[data.size()]);
        Arrays.sort(array);
        StringBuilder builder = new StringBuilder();
        for (String key : array) {
            try {
                builder.append(key).append("=").append(URLEncoder.encode(data.get(key), "UTF-8")).append("&");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return DigestUtils.md5DigestAsHex((appSecret + builder.toString()).getBytes()).equals(sign);
    }

    /**
     * 参数校验测试
     */
    @Test
    public void test() {
        String sign = sign("1000119000002894316", "靳海军","13823116872","sinopec",100,APP_KEY,APP_SECRET);
        //appKey=appKey&appSecret=appSecret&money=1000&type=ZHY&sign=7def5ba075329d7616888a59e63383a4
        //gasnumber=1000119000002894316&gasname=靳海军&gasphone=13823116872&gastype=sinopec&money=100
        boolean check = check("1000119000002894316", "靳海军","13823116872","sinopec",100, "7def5ba075329d7616888a59e63383a4");
        System.out.println(check);
    }

}
